{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "unusual-large",
   "metadata": {},
   "source": [
    "# German Tax Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "mighty-malaysia",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "concerned-windsor",
   "metadata": {},
   "source": [
    "The function `clean_de_stnr()` cleans a column containing German tax numbers (STNR) strings, and standardizes them in a given format. The function `validate_de_stnr()` validates either a single STNR strings, a column of STNR strings or a DataFrame of STNR strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "vocal-gossip",
   "metadata": {},
   "source": [
    "STNR strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"18181508155\"\n",
    "* `standard`: STNR strings with proper whitespace in the proper places, like \"181/815/0815 5\"\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_de_stnr()` and `validate_de_stnr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "educated-defeat",
   "metadata": {},
   "source": [
    "### An example dataset containing STNR strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "precise-digit",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"stnr\": [\n",
    "            ' 181/815/0815 5',\n",
    "            \"136695978\",\n",
    "            \"201/123/12340\",\n",
    "            \"4151081508156\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\"\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "given-veteran",
   "metadata": {},
   "source": [
    "## 1. Default `clean_de_stnr`\n",
    "\n",
    "By default, `clean_de_stnr` will clean stnr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "trying-tourism",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_de_stnr\n",
    "clean_de_stnr(df, column = \"stnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "neutral-florence",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "finnish-retail",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "chicken-happening",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "variable-petersburg",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_stnr(df, column = \"stnr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "contained-recipe",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "retained-thinking",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_stnr(df, column = \"stnr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "emotional-trance",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned STNR strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "uniform-sword",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_stnr(df, column=\"stnr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "olive-helping",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "muslim-bronze",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "compatible-favorite",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_stnr(df, \"stnr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "exempt-watson",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "recognized-offering",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_de_stnr(df, \"stnr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "incorporated-channel",
   "metadata": {},
   "source": [
    "## 4. `validate_de_stnr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "billion-waste",
   "metadata": {},
   "source": [
    "`validate_de_stnr()` returns `True` when the input is a valid STNR. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_de_stnr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_de_stnr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_de_stnr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "behavioral-tournament",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_de_stnr\n",
    "print(validate_de_stnr(\"181/815/0815 5\"))\n",
    "print(validate_de_stnr(\"136695978\"))\n",
    "print(validate_de_stnr(\"201/123/12340\"))\n",
    "print(validate_de_stnr(\"4151081508156\"))\n",
    "print(validate_de_stnr(\"hello\"))\n",
    "print(validate_de_stnr(np.nan))\n",
    "print(validate_de_stnr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "downtown-excuse",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "organized-scottish",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_stnr(df[\"stnr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "consecutive-minority",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "artistic-lambda",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_stnr(df, column=\"stnr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "offensive-nursing",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "thirty-singles",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_stnr(df)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "formed-stations",
   "metadata": {},
   "source": [
    "## `region` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "further-holder",
   "metadata": {},
   "source": [
    "Specifically, `region` can be supplied to `validate_de_stnr` to verify that the number is assigned in that region."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "working-being",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_stnr(df[\"stnr\"], region='Sachsen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disciplinary-anthropology",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_de_stnr(df[\"stnr\"], region='Thuringen')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "wound-circulation",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
